<?php

namespace app\models\sdiot;

use Yii;
use yii\base\Exception;
use yii\base\Model;

/**
 * 山东移动物联网接口
 * Class Api
 * @package app\models\sdiot
 */
class Api extends Model
{
    /**
     * @var string 服务器
     */
    private $server;
    /**
     * @var string APPKEY
     */
    private $appKey;
    /**
     * @var string APPSECRET
     */
    private $appSecret;
    /**
     * @var string Token
     */
    private $token;
    /**
     * @var string 短信接入码
     */
    private $smsSpCode;
    /**
     * @var string 企业代码
     */
    private $smsSpId;
    /**
     * @var string 业务代码
     */
    private $smsServiceId;
    /**
     * @var string 业务网关登录用户名
     */
    private $smsLoginName;
    /**
     * @var string 业务网关登录密码
     */
    private $smsPassword;

    /**
     * @inheritdoc
     * @param string $ispData isp.data
     */
    public function __construct($ispData)
    {
        $json = json_decode($ispData, true);
        $this->server = isset($json['server']) ? $json['server'] : '';
        $this->appKey = isset($json['appKey']) ? $json['appKey'] : '';
        $this->appSecret = isset($json['appSecret']) ? $json['appSecret'] : '';
        $this->token = hash('sha256', $this->appKey . $this->appSecret);
        $this->smsSpCode = isset($json['smsSpCode']) ? $json['smsSpCode'] : '';
        $this->smsSpId = isset($json['smsSpId']) ? $json['smsSpId'] : '';
        $this->smsServiceId = isset($json['smsServiceId']) ? $json['smsServiceId'] : '';
        $this->smsLoginName = isset($json['smsLoginName']) ? $json['smsLoginName'] : '';
        $this->smsPassword = isset($json['smsPassword']) ? $json['smsPassword'] : '';
        parent::__construct([]);
    }

    use ApiBossTrait, ApiCmiotTrait, ApiRechargeTrait, ApiSdiotTrait, ApiSmsTrait;

    /**
     * @see Api::post()
     * @param string $uri 接口地址
     * @param array $json 数据
     * @return array
     * @throws Exception
     */
    private function post1($uri, $json)
    {
        return $this->post($uri, $json, true);
    }

    /**
     * @see Api::post()
     * @param string $uri 接口地址
     * @param array $json 数据
     * @return array
     * @throws Exception
     */
    private function post2($uri, $json)
    {
        return $this->post($uri, $json, false);
    }

    /**
     * 调用方法
     * @param string $uri 接口地址
     * @param array $json 数据
     * @param boolean $checkHttpStatus 是否检查Http头信息
     * @return array
     * @throws Exception
     */
    private function post($uri, $json, $checkHttpStatus = true)
    {
        $json['appkey'] = $this->appKey;
        $json['token'] = $this->token;
        $url = $this->server . $uri;
        $data = json_encode($json);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json; charset=utf-8',
        ]);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $resp = curl_exec($curl);
        $error_no = curl_errno($curl);
        $error = curl_error($curl);
        curl_close($curl);

        if (YII_DEBUG) {
            Yii::warning($url, 'sdiot');
            Yii::warning($data, 'sdiot');
            Yii::warning($resp, 'sdiot');
        }
        if ($error_no != 0) {
            throw new Exception('接口错误：' . $error_no . '：' . $error);
        }

        $json = json_decode($resp, true);
        if ($json === null) {
            throw new Exception('无法解析接口返回数据：' . $resp);
        }

        if ($checkHttpStatus) {
            if (!isset($json['HttpStatus']) || $json['HttpStatus'] != 200) {
                throw new Exception('返回异常：' . $resp);
            }
            return $json['HttpEntity'];
        }

        return $json;
    }
}
